package com.blockmeta.bbs.baselibrary.utils.loading;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blockmeta.bbs.baselibrary.R;


/**
 * Description：自定义等待圈显示与隐藏
 * <br/><br/>Created by Fausgoal on 15/9/14.
 * <br/><br/>
 */
public class GLCustomProgressDialog extends Dialog {
    private final TextView tvProgressMsg;

    public GLCustomProgressDialog(Context context, String msg) {
        this(context, R.style.GLCustomProgressDialog, msg);
    }

    public GLCustomProgressDialog(Context context, int theme, String msg) {
        super(context, theme);


        this.setContentView(R.layout.progress_dialog);
        this.getWindow().getAttributes().gravity = Gravity.CENTER;

        ImageView ivLoading = (ImageView) this.findViewById(R.id.ivLoading);
        tvProgressMsg = (TextView) this.findViewById(R.id.tvProgressMsg);

        // 加载动画
        AnimationDrawable animationDrawable = (AnimationDrawable) ivLoading.getBackground();
        // 使用ImageView显示动画
        animationDrawable.stop();
        animationDrawable.start();

        setProgressMsg(msg);
    }

    public void setProgressMsg(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            if (null != tvProgressMsg) {
                tvProgressMsg.setVisibility(View.VISIBLE);
                tvProgressMsg.setText(msg);
            }
        } else {
            if (null != tvProgressMsg) {
                tvProgressMsg.setVisibility(View.GONE);
            }
        }
    }

    public void setProgressMsg(int msgRedIs) {
        setProgressMsg(getContext().getString(msgRedIs));
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if (!hasFocus) {
            dismiss();
        }
    }
}
